package com.mouliu.base.syn006;

/**
 * 锁对象的改变
 * @author liu.mou
 */
public class ChangeLock {
    private String lock = "lock";
    private void method(){
        //锁住的是一段代码
        synchronized (lock){
//            try {
//                System.out.println("当前线程 : "  + Thread.currentThread().getName() + "开始");
//                lock = "change lock";
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println("test开始..");
            //锁对象改变
            lock = "change lock";
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("test结束..");
        }
    }
    public static void  main(String[] args){
        final ChangeLock changeLock = new ChangeLock();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                changeLock.method();
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                changeLock.method();
            }
        },"t2");
        t1.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}
